package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.search.query.SearchQueryObject;
import cn.tedu.boyou.search.service.*;
import cn.tedu.boyou.search.template.DestinationTemplate;
import cn.tedu.boyou.search.template.StrategyTemplate;
import cn.tedu.boyou.search.template.TravelTemplate;
import cn.tedu.boyou.search.template.UserInfoTemplate;
import cn.tedu.boyou.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SearchController {
    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("/q")
    public String query(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        //type -1 全部
        //type 0  目的地
        //type 1  攻略
        //type 2  游记
        //type 3  用户
        switch (qo.getType()){
            case 0://目的地
                return destSearch(model,qo);
            case 1: //攻略
                return strategy(model,qo);
            case 2: //游记
                return travelSearch(model,qo);
            case 3: //用户
                return userSearch(model,qo);
            default: //全部
                return allSearch(model,qo);
        }
    }

    //目的地索引
    public String destSearch(Model model,SearchQueryObject qo){
        //1：根据关键字查询目的地对象
        DestinationTemplate dest = destinationSearchService.queryDestByName(qo.getKeyword());

        if(dest != null){
            //如果存在，查询该目的地所有的攻略，游记，用户
            qo.setPageSize(5);
            qo.setDestId(dest.getId());
           Page<TravelTemplate> travels =  travelSearchService.query(qo);

            Page<StrategyTemplate> strategys = strategySearchService.queryByDestId(qo);

            Page<UserInfoTemplate> users = userInfoSearchService.queryByDestName(dest.getName());

            SearchResultVO vo = new SearchResultVO();
            vo.setStrategys(strategys.getContent());
            vo.setTravels(travels.getContent());
            vo.setUsers(users.getContent());
            //data
            long total = travels.getTotalElements() + strategys.getTotalElements() + users.getTotalElements();
            vo.setTotal(total);
            model.addAttribute("data",vo);
        }
        //dest
        model.addAttribute("dest",dest);
        //如果不存在，直接提示查询条数 为0
        return "index/searchDest";
    }
    //攻略索引
    public String strategy(Model model,SearchQueryObject qo){
        //攻略
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle","summary");
        model.addAttribute("page",strategys);
        return "index/searchStrategy";
    }
    //游记索引
    public String travelSearch(Model model,SearchQueryObject qo){
        //游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",travels);
        return "index/searchTravel";
    }
    //用户索引
    public String userSearch(Model model,SearchQueryObject qo){
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",users);
        return "index/searchUser";
    }
    //全部索引
    public String allSearch(Model model,SearchQueryObject qo){
        //目的
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,
                DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        //攻略
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle","summary");

        //游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        //用户
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        SearchResultVO vo = new SearchResultVO();
        vo.setStrategys(strategys.getContent());
        vo.setTravels(travels.getContent());
        vo.setUsers(users.getContent());
        vo.setDests(dests.getContent());
        //data
        long total = travels.getTotalElements() + strategys.getTotalElements() + users.getTotalElements() + dests.getTotalElements();
        vo.setTotal(total);
        model.addAttribute("data",vo);
        return "index/searchAll";
    }
}
